package com.example.b_308.myapplication.view.user.register;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.b_308.myapplication.R;
import com.example.b_308.myapplication.controller.UserController;
import com.example.b_308.myapplication.model.Company;
import com.example.b_308.myapplication.model.Department;
import com.example.b_308.myapplication.model.Staff;
import com.example.b_308.myapplication.view.base.BaseFragment;
import com.example.b_308.myapplication.view.user.login.IUserView;
import com.example.b_308.myapplication.view.user.register.createdepartment.DepartmentAdapter;
import com.example.b_308.myapplication.view.user.register.createdepartment.UpdateDepartmentActivity;

import java.util.ArrayList;

/**
 * Created by B-308 on 2017/11/23.
 */
public class ConsummateFragment extends BaseFragment implements View.OnClickListener, IUserView {
    LinearLayout ll_add_department;
    Button but_next_com;
    EditText et_com_name, et_com_tel, et_com_address, et_com_scale;
    ListView lv_company;
    DepartmentAdapter departmentAdapter;
    ArrayList<String> departmentList = new ArrayList<>();
    UserController userController;

    @Override
    protected void init() {
        ll_add_department = findViewById(R.id.ll_add_department);
        but_next_com = findViewById(R.id.but_next_com);
        et_com_name = findViewById(R.id.et_com_name);
        et_com_tel = findViewById(R.id.et_com_tel);
        et_com_address = findViewById(R.id.et_com_address);
        et_com_scale = findViewById(R.id.et_com_scale);
        lv_company = findViewById(R.id.lv_company);
        departmentAdapter = new DepartmentAdapter(UserController.departmentList, getActivity());
        lv_company.setAdapter(departmentAdapter);

        ll_add_department.setOnClickListener(this);
        but_next_com.setOnClickListener(this);
    }

    @Override
    protected View setContentView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_consummate, null, false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.but_next_com:
                Company company = new Company();
                company.setName(et_com_name.getText().toString());
                company.setTelephone(et_com_tel.getText().toString());
                company.setAddress(et_com_address.getText().toString());
                company.setScale(Integer.parseInt(et_com_scale.getText().toString()));
                if (et_com_name.length() != 0 && et_com_tel.length() != 0 && et_com_address.length() != 0 && et_com_scale.length() != 0) {
                    but_next_com.setSelected(true);
                    ((RegisterActivity) getActivity()).next(this, company);
                }else {
                    fail("请完善企业信息！");
                }
                break;
            case R.id.ll_add_department:
                ((RegisterActivity) getActivity()).tentForResult(UpdateDepartmentActivity.class, 2);
                break;
        }
    }

    @Override
    public void loginSuccess(Staff staff) {

    }

    @Override
    public void registerSuccess() {

    }

    @Override
    public void getCodeSuccess() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        final Department department;
        if (2 == requestCode) {
            UserController.departmentList.add(new Department(data.getStringExtra(Department.TAG)));
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    departmentAdapter.notifyDataSetChanged();
                }
            });
        }
    }
}
